package controllers

import (
	"fmt"
	"gin-gorm-learn/client"
	"gin-gorm-learn/model"
	"github.com/gin-gonic/gin"
)

type TaskController struct {
}

// task

func (t TaskController) AutoMigrate(c *gin.Context) {
	result := client.MysqlTask{}.AutoMigrate()
	if result.Error != nil {
		// autoMigrate failed
		c.JSON(500, model.Resp{}.Error(result.Error))
		return
	}

	c.JSON(200, model.Resp{}.Ok(result))
}

func (t TaskController) Insert(c *gin.Context) {
	json := model.VideoInfo{}
	err := c.BindJSON(&json)
	if err == nil {
		// error == nil: success get json
		result, err := client.MysqlTask{}.InsertItem(&json)
		fmt.Printf("TaskController.Insert json=%#v", json)
		if err != nil {
			c.JSON(500, model.Resp{}.Error(err))
			return
		}
		c.JSON(200, model.Resp{}.Ok(result))
		return
	}

	fmt.Println("receive json error:", err)
	c.JSON(500, model.Resp{}.Error(err.Error))
}

// about db

func (t TaskController) ReConnect(c *gin.Context) {
	result, err := client.MysqlTask{}.ReConnect()
	if err != nil {
		c.JSON(500, model.Resp{}.Error(err))
	}

	c.JSON(200, model.Resp{}.Ok(result))

}
